package com.obtk.byproperty.service.impl;

import com.obtk.byproperty.dao.RepairMapper;
import com.obtk.byproperty.pojo.Repair;
import com.obtk.byproperty.service.RepairService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class RepairServiceImpl implements RepairService {

    @Autowired
    private RepairMapper repairMapper;

    @Override
    public List<Repair> selectByName(String ownerName,String status) {
        return repairMapper.selectByName(ownerName,status);
    }

    @Override
    public List<Repair> selectDeviceName(String ownerName,String deviceName) {
        return repairMapper.selectDeviceName(ownerName,deviceName);
    }

    @Override
    public List<String> getBuildingName() {
        return repairMapper.getBuildingName();
    }

    @Override
    public Boolean addRepair(Repair repair) {
        int row = repairMapper.insert(repair);
        if(row>0){
            return true;
        }
        return false;
    }

    @Override
    public Boolean deleteRepairId(Integer repairId) {
        int row = repairMapper.deleteByPrimaryKey(repairId);
        if(row>0){
            return true;
        }
        return false;
    }

    @Override
    public Repair selectById(Integer repairId) {
//        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//        HttpServletRequest request = requestAttributes.getRequest();
//
//        String localAddr = request.getLocalAddr();
//        int serverPort = request.getServerPort();
        Repair repair = repairMapper.selectByPrimaryKey(repairId);
//        repair.setRepairImage("http://" + localAddr + ":" + serverPort + "/activity/" + repair.getRepairImage());
//        System.out.println(repair.getRepairImage());
        return repair;
    }
}
